package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.mapper.TbItemCatMapper;
import com.dongdong.mapper.TbItemMapper;
import com.dongdong.pojo.TbItem;
import com.dongdong.pojo.TbItemCat;
import com.dongdong.pojo.TbItemCatExample;
import com.dongdong.pojo.TbItemExample;
import com.dongdong.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public List<TbItemCat> selectInfo(Long parenId) {
        TbItemCatExample te = new TbItemCatExample();
        te.createCriteria().andParentIdEqualTo(parenId);
        return tbItemCatMapper.selectByExample(te);
    }

    @Override
    public List<TbItemCat> selectAll() {
        return tbItemCatMapper.selectByExample(null);
    }

    @Override
    public TbItemCat selectById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateInfo(TbItemCat tbItemCat) {
        return tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    public int insert(TbItemCat tbItemCat) {
        return tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        int i = 0;
        for (Long id:ids) {
            tbItemCatMapper.deleteByPrimaryKey(id);
            i= tbItemCatMapper.deleteById(id);
        }
       return i;
    }

    @Override
    public List<TbItem> selectByGoodsId(Long goodsId) {
        TbItemExample tb = new TbItemExample();
        tb.createCriteria().andGoodsIdEqualTo(goodsId).andStatusEqualTo("1").andStockCountGreaterThan(0);
        return tbItemMapper.selectByExample(tb);
    }

    @Override
    public TbItem getItemById(Long itemId) {

        return tbItemMapper.selectByPrimaryKey(itemId);
    }
}

